<?php

namespace Guestbook;

/**
 * 留言板业务相关
 */
class GuestbookModel extends \Business\BusinessBase
{
    /**
     * 列表功能
     *
     * @param array $params
     * @return array
     */
    public function getList($params)
    {
        $page = empty($params['page']) ? 1 : $params['page'];
        $count = empty($params['count']) ? 10 : $params['count'];
        $where = empty($params['where']) ? null : $params['where'];
        $order = empty($params['order']) ? null : $params['order'];
        $baseUrl = empty($params['baseUrl']) ? null : $params['baseUrl'];

        $GuestbookDAOModel = GuestbookDAOModel::getInstance();
        $totalArr = $GuestbookDAOModel->fetchAll(array(new \Zend\Db\Sql\Expression('count(0) as total')), $where, null, 1);
        $totalNum = $totalArr[0]['total']; //总数

        $paginator = new \Our\Paginator($page, $totalNum, $count, array('baseUrl' => $baseUrl, 'ext' => '/'));
        $result = $GuestbookDAOModel->fetchAll(null, $where, array('guestbook_id desc'), $count, ($page - 1) * $count);

        return [$result, $paginator];
    }

    /**
     * 类实例
     *
     * @var \Guestbook\GuestbookModel
     */
    private static $_instance = null;

    /**
     * 获取类实例
     *
     * @return \Guestbook\GuestbookModel
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}
